package org.afun.csms_android;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.afun.csms_android.domain.User;
import org.afun.csms_android.utils.MyDatabaseHelper;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;


public class LoginActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    private EditText account;
    private EditText password;
    private Button log;
    private Button sign;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = this;
        dbHelper = new MyDatabaseHelper(this);
        SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
        // 更新数据库
        dbHelper.onUpgrade(sqLiteDatabase,1,2);

        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        log = findViewById(R.id.login);
        sign = findViewById(R.id.signin);

        log.setOnClickListener(new View.OnClickListener() {         //点击登录，跳转到MainActivity,传入对应id
            @Override
            public void onClick(View view) {
                String id_str = account.getText().toString();
                // int id = Integer.parseInt(id_str);
                String passwd = password.getText().toString();
                MyTableOperator myTableOperator = new MyTableOperator(mContext);
                TableOperator tableOperator = myTableOperator.getTableOperator();
                if (!EditCheck.CheckInt(id_str, "账号", 0, 99999)) {
                    showNormalDialog(EditCheck.getWarning());
                } else if (!EditCheck.CheckString(passwd, "密码", 20)) {
                    showNormalDialog(EditCheck.getWarning());
                } else if (tableOperator.check(id_str, passwd)) {              //账号匹配成功,进入MainActivity
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    User user = tableOperator.findByUserAccount(id_str);
                    // 传递user
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("user", user);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                } else {                              //弹出错误提示框
                    String word = "账号或密码错误！";
                    showNormalDialog(word);
                }

            }
        });

        sign.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, ModifyPasswdActivity.class);
                startActivity(intent);
            }
        });
    }

    private void showNormalDialog(String word) {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(LoginActivity.this);
        normalDialog.setTitle("提示");
        normalDialog.setMessage(word);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        normalDialog.setCancelable(true);
                        //...To-do
                    }
                });

        // 显示
        normalDialog.show();
    }
}

